package com.shop.loan.redis;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;

@Component
public class ExpireLock {

    @Resource
    RedisUtil redisUtil;

    /**
     * 给key续命
     *
     * @param
     * @param lockKey
     */
    @Async
    public void expireLock(String lockKey, String lockValue, Long time, Long maxTime) {
        Jedis jedis = redisUtil.getJedis();
        try {
            long waitTime = 0;
            while (lockValue.equals(jedis.get(lockKey))) {
                if (maxTime == null || waitTime < maxTime) {
                    jedis.expire(lockKey, time.intValue());
                    long diff = time / 3;
                    Thread.sleep(diff * 1000);

                    waitTime = waitTime + diff * 1000;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }


}
